﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.CRM.BigCustomerTradeAnalysis;

namespace BCData.CRM.BigCustomerTradeAnalysis
{
    public class BigCustomerTradeAnalysisData : IBigCustomerTradeAnalysisData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    A.BigCustomerTradeAnalysisId,
		    A.BigCustomerId,
		    A.CompanyId,
		    A.UserId,
		    A.TradeTimes,
		    A.TradeAmount,
		    A.YearTradeTimes,
		    A.YearTradeAmount,
		    A.LatestTradeTime,
		    A.LatestTradeAmount,
		    A.AverageTradeCycle,
		    A.AverageTradeAmount,
		    A.ComplaintTimes,
		    A.ClosedComplaintTimes,
		    A.RepairTimes,
		    A.CompletedRepairTimes,
		    A.ReturnGoodsTimes,
		    A.ReturnGoodsSignTimes,
		    A.ReturnGoodsAmount,
		    A.YearComplaintTimes,
		    A.YearClosedComplaintTimes,
		    A.YearRepairTimes,
		    A.YearCompletedRepairTimes,
		    A.YearReturnGoodsTimes,
		    A.YearReturnGoodsSignTimes,
		    A.YearReturnGoodsAmount,
		    A.YearRefundNoReturnGoodsAmount,
		    A.RefundNoReturnGoodsAmount,
		    A.CreateTime,
		    A.UpdateTime,
        FROM
            BigCustomerTradeAnalysis A";



        #region 构造函数
        public BigCustomerTradeAnalysisData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新大客户交易分析纪录
        /// </summary>
        /// <param name="bigCustomerTradeAnalysisEntity"></param>
        /// <returns></returns>
        public async Task<BigCustomerTradeAnalysisEntity> InsertAsync(BigCustomerTradeAnalysisEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改大客户交易分析记录
        /// </summary>
        /// <param name="bigCustomerTradeAnalysisEntity"></param>
        /// <returns></returns>
        public async Task<BigCustomerTradeAnalysisEntity> UpdateAsync(BigCustomerTradeAnalysisEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除大客户交易分析记录
        /// </summary>
        /// <param name="bigCustomerTradeAnalysisId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long bigCustomerTradeAnalysisId)
        {
            string sql = "DELETE FROM crm_bigcustomertradeanalysis WHERE BigCustomerTradeAnalysisId = ?bigCustomerTradeAnalysisId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BigCustomerTradeAnalysisId", bigCustomerTradeAnalysisId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取大客户交易分析记录
        /// </summary>
        /// <param name="bigCustomerTradeAnalysisId"></param>
        /// <returns></returns>
        public async Task<BigCustomerTradeAnalysisEntity> GetEntityAsync(long bigCustomerTradeAnalysisId)
        {
            string sql = QUERY + " WHERE  A.BigCustomerTradeAnalysisId = ?bigCustomerTradeAnalysisId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BigCustomerTradeAnalysisId", bigCustomerTradeAnalysisId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<BigCustomerTradeAnalysisEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询大客户交易分析记录
        public async Task<(IEnumerable<BigCustomerTradeAnalysisEntity>, int)> GetListAsync(int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();


            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  crm_bigcustomertradeanalysis  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.database.Slave.QueryPageableListAsync<BigCustomerTradeAnalysisEntity>(sql, parameters);
        }
        #endregion

    }
}
//接口



